//
//  addEvaluateViewController.m
//  ZhenPin
//
//  Created by tmosport on 14-7-16.
//  Copyright (c) 2014年 zhenpin. All rights reserved.
//

#import "addEvaluateViewController.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"

@interface addEvaluateViewController ()
@property (nonatomic, retain) NSArray *logResult;
@property (strong, nonatomic) IBOutlet UIImageView *pImg;
@property (strong, nonatomic) IBOutlet UILabel *bName;
@property (strong, nonatomic) IBOutlet UILabel *productName;
@property (strong, nonatomic) IBOutlet UILabel *placeHolerLabel;
@property (strong, nonatomic) IBOutlet UITextView *evaTF;
@end

@implementation addEvaluateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self dataInit];
    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]
                                             initWithTitle:@"保存"
                                             style:UIBarButtonItemStyleBordered
                                             target:self
                                             action:@selector(toSend)];
    _evaTF.layer.cornerRadius = 4.0f;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragraphStyle setLineSpacing: 3];
    NSString *placeText = @"评价内容长度请控制在5--100字之间，可以将您的使用感受、购物建议、实物照片、拆包过程与大家分享。";
    NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyle};
    
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:placeText attributes:attributes];
    
    _placeHolerLabel.attributedText = attributedString;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]   initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
}

-(void)dismissKeyboard {
    [_evaTF resignFirstResponder];
}

- (void)dataInit{

    NSDictionary *params = @{@"memberid": _memberId,
                             @"access_token": _token,
                             @"orderitemid": [NSNumber numberWithInt:_orderitemid]
                             };
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:APIURL@"myzhenpin/getOrderItemDtail.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        _logResult = responseObject;
        [self refrshView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //TODO:错误捕获
    }];
}

-(void)refrshView
{
    
     NSDictionary *contentDic = [_logResult objectAtIndex:0];
    _bName.text = contentDic[@"brandname"];
    _productName.text = contentDic[@"productName"];
    [_pImg sd_setImageWithURL:Return_imageUrl(contentDic[@"imageSource"], 180) placeholderImage:[UIImage imageNamed:@"noimage"] options:SDWebImageRetryFailed];
}

- (void)toSend{
    NSDictionary *varDic = [_logResult objectAtIndex:0];
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.dimBackground = YES;
    HUD.labelText = @"发送评论";
    NSString *evaluatecontent = _evaTF.text;
    if ([evaluatecontent length] == 0||[evaluatecontent isEqual:[NSNull null]]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请正确填写评论" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [HUD hide:YES];
        return;
    }
    NSDictionary *newparm = @{@"memberid": _memberId,
                             @"access_token": _token,
                             @"orderid": [NSString stringWithFormat:@"%@", varDic[@"orderId"]],
                              @"orderitemid": [NSNumber numberWithInt:_orderitemid],
                             @"productid": [NSString stringWithFormat:@"%@",varDic[@"productId"]],
                             @"productspecid": [NSString stringWithFormat:@"%@",varDic[@"productSpecId"]],
                             @"evaluatecontent": evaluatecontent,
                             @"score":@"5"
                             };
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger POST:APIURL@"myzhenpin/evaluate/addEvaluate.json" parameters:newparm success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *logBack = responseObject;
        if ([logBack[@"isOk"] intValue] == 1) {
            [HUD showAnimated:YES whileExecutingBlock:^{
                sleep(1);
                HUD.mode = MBProgressHUDModeText;
                HUD.labelText = @"发送成功!";
            }completionBlock:^{
                sleep(1);
                [HUD hide:YES afterDelay:1];
                [self.navigationController popViewControllerAnimated:YES];
            }];

        }else{
        
            [HUD showAnimated:YES whileExecutingBlock:^{
                HUD.mode = MBProgressHUDModeText;
                HUD.labelText = @"发送失败!";
            }completionBlock:^{
                sleep(1);
                [HUD hide:YES afterDelay:1];
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //DO:接口回传失败日志
    }];
}

-(void)textViewDidChange:(UITextView *)textView{
    if ([_evaTF.text length] == 0) {
        _placeHolerLabel.hidden = NO;
    }else{
        _placeHolerLabel.hidden = YES;
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
